define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Newproductpost_test,$stateParams,GoDialog,$ionicSlideBoxDelegate,$interval,GoPage) {
		
        //接收参数
		$scope.act_id = $stateParams.act_id;
		$scope.act_type = $stateParams.act_type;
		//格式化时间
		
		$scope.init_time = function(data, now_time){
			if(data.now_time != undefined)
					data.now_time = data.now_time - 1;
				else{
					if($scope.act_type==1){
						data.start_time = data.start_time - now_time;
						$scope.act_time_name = '距开始';
					}else{
						data.now_time = data.end_time - now_time;
						$scope.act_time_name = '距结束';
					}
				}
					
				data.formate_time = $scope.formate_time(data.now_time);
		}
		$scope.set_timer = function(now_time) {
			$interval(function() {
				if($scope.detail.now_time != undefined)
					$scope.detail.now_time = $scope.detail.now_time - 1;
				else{
					if($scope.act_type==1){
						$scope.detail.now_time = $scope.detail.start_time - now_time;
					}else{
						$scope.detail.now_time = $scope.detail.end_time - now_time;
					}
				}
					
				$scope.detail.formate_time = $scope.formate_time($scope.detail.now_time);
			}, 1000)
		}
		$scope.formate_time = function(time) {
//			console.log(time);
			if(time > 0) {
				var day = parseInt(time / (3600 * 24));
				var hour = parseInt((time % (3600 * 24)) / 3600);
				if(hour < 10)
					hour = "0" + hour;
				var min = parseInt((time % 3600) / 60);
				if(min < 10)
					min = "0" + min;
				var second = time % 60;
				if(second < 10)
					second = "0" + second;
				return day + '天' + hour + '小时' + min + '分' + second + '秒';
			} else {
				return '0天0小时0分0秒';
			}
		};
		$scope.data_init = function(){
			Newproductpost_test.detail($scope.act_id,function(data,now_time) {
				$scope.detail = data;
				console.log(data);
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
				$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
				$scope.init_time(data,now_time);
				$scope.set_timer(now_time);
			});
			Newproductpost_test.winninglist($scope.act_id,function(data){
				$scope.winnerlist = data;
				console.log(data)
			});
		}
		$scope.data_init();//初始化数据
		
		//跳转到商品详情
		$scope.onshow_detail = function(goods_id){
			GoPage.go_states("newpropromotions_goods",{"goods_id":goods_id,"act_id":$scope.act_id});
		}
		
		$scope.isHide = true;//默认隐藏
		//隐藏弹窗
		$scope.hide_apply = function(){
			$scope.isHide = true;
		}
		//显示弹窗
		$scope.show_apply = function(){
			$scope.isHide = false;
		}
		
		/*--------------文件上传----------*/
		$scope.uploadFile = function(){
			var fileInput = document.getElementById("paper");
			fileInput.click();
		}
		$scope.onuploadFile = function(val){
			var fileList = val.files;
			var reader = new FileReader();
			reader.readAsDataURL(fileList[0]);
			var obj = document.getElementById('paper');
			//文件名
			$scope.$apply(function(){
				$scope.filename = fileList[0].name;
			})
			
			obj.value="";//清除控件value值
		}
		
		//提交申请
		$scope.apply = function(){
			Newproductpost_test.apply($scope.act_id,function(data) {
				if(data.success){
					GoDialog.toast("申请成功！");
				}else{
					GoDialog.toast(data.msg);
				}
			});
		}
		
		//商品详情
		
		
		
    }

	controller.$inject = ['$scope', 'Newproductpost_test','$stateParams','GoDialog','$ionicSlideBoxDelegate','$interval','GoPage'];

	return app.registerController("newproductpost_testctrl",controller);
});